<template>
  <div class="mod-config">
    <el-row>
      <!-- <span>系统通知</span> -->
      <el-button size="mini" @click="getDataList()" style="margin: -40px 0 10px 0; float: right;">刷新</el-button>
    </el-row>

    <el-table
      :data="dataList"
      v-loading="dataListLoading"
      :header-cell-style="{background:'#E7EFFC',color:'#48494D'}"
      :row-class-name="tabRowClassName"
      style="width: 100%;"
    >
      <!-- <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column> -->
      <!-- <el-table-column prop="id" header-align="center" align="center" label="id"></el-table-column> -->
      <el-table-column prop="title" header-align="center" align="center" label="标题"></el-table-column>
      <el-table-column prop="content" header-align="center" align="center" label="通知内容">
        <template slot-scope="{row}">
          <span v-if="row.content != null && row.content.length <= 18" 
            style="cursor: pointer;"
            @click="detailHandle(row.id)">
            {{row.content}}
          </span>
          <span
            style="cursor: pointer;"
            @click="detailHandle(row.id)" 
            v-else-if="row.content != null && row.content.length > 18">
            {{row.content.substring(0,18)}}...
          </span>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="orderNum" header-align="center" align="center" label="排序"></el-table-column> -->
      <el-table-column prop="createTime" header-align="center" align="center" label="发布时间">
        <template slot-scope="{row}">{{dateFormat(row.createTime, 'YYYY/MM/DD hh:mm')}}</template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            v-if="isAuth('sys:notice:all')"
            size="small"
            @click="detailHandle(scope.row.id)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <NoticeDetail v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></NoticeDetail>
  </div>
</template>

<script>
import NoticeDetail from "./notice-detail";
export default {
  data() {
    return {
      dataForm: {
        title: ""
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    };
  },
  components: {
    NoticeDetail
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/sys/notice/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          title: this.dataForm.title,
          status: 2
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 新增 / 修改
    detailHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },

    tabRowClassName({ row, rowIndex }) {
      let index = rowIndex + 1;
      if (index % 2 == 0) {
        return "notice-row";
      }
    }
  }
};
</script>
<style scoped>
.el-table >>> .notice-row {
  background-color: #E7EFFC;
}
</style>